package com.gin.no_gay_alliance.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.gin.jackson.utils.JacksonUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.databinding.ActivityTopicListBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.topic.BaseTopicListFragment;
import com.gin.no_gay_alliance.fragment.topic.FavorFragment;
import com.gin.no_gay_alliance.fragment.topic.ForumRecommendFragment;
import com.gin.no_gay_alliance.fragment.topic.TopicSearchFragment;
import com.gin.no_gay_alliance.fragment.topic.UserSearchFragment;
import com.gin.no_gay_alliance.fragment.topic.list.ColListFragment;
import com.gin.no_gay_alliance.fragment.topic.list.ForumListFragment;
import com.gin.no_gay_alliance.param.TopicListParam;

/**
 * 主题列表活动v2
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/5 00:46
 **/
public class TopicListActivity extends BaseBindingActivity<ActivityTopicListBinding> {
    public static final String ARG_PARAM_KEY = TopicListParam.class.getName();
    TopicListParam param;
    BaseTopicListFragment<? extends ViewBinding> fragment;

    @Override
    protected void handleIntent(Intent intent) {
        param = intent.getParcelableExtra(ARG_PARAM_KEY);
        // 尝试从链接中解析
        if (param == null) {
            String url = intent.getDataString();
            if (!ObjectUtils.isEmpty(url)) {
                param = TopicListParam.parseUrl(url);
            }
        }
        if (param == null) {
            throw new RequireParamException();
        }
        JacksonUtils.print(param);
    }

    @Override
    protected void onCreate() {
        switch (param.type) {
            case FAVOR:
                fragment = new FavorFragment();
                break;
            case COL_LIST:
                fragment = new ColListFragment();
                break;
            case FORUM_LIST:
                fragment = new ForumListFragment();
                break;
            case FORUM_SEARCH:
            case COL_SEARCH:
                fragment = new TopicSearchFragment();
                break;
            case FORUM_RECOMMEND:
                fragment = new ForumRecommendFragment();
                break;
            case USER_SEARCH:
                fragment = new UserSearchFragment();
                break;
            default:
                throw new RuntimeException("类型未实现: " + param.type);
        }
        fragment.setArguments(param);
        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {

    }

    @Override
    public ActivityTopicListBinding createViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        // 不返回binding 使用Fragment.replace
        return null;
    }

    /**
     * 使用网址启动本活动
     *
     * @param context 上下文
     * @param url     url
     */
    public static void startActivity(Context context, String url) {
        Intent intent = new Intent(context, TopicListActivity.class);
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
    }

    /**
     * 使用参数启动活动
     *
     * @param context 上下文
     * @param param   参数
     */
    public static void startActivity(Context context, TopicListParam param) {
        Intent intent = new Intent(context, TopicListActivity.class);
        intent.putExtra(ARG_PARAM_KEY, param);
        context.startActivity(intent);
    }

    /**
     * 打开我的收藏夹
     *
     * @param context 上下文
     */
    public static void startActivityFavor(Context context) {
        TopicListParam param = new TopicListParam();
        param.type = TopicListParam.Type.FAVOR;
        param.authorUid = MyApplication.getClient().getUserId();
        startActivity(context, param);
    }
}
